/** 
 * @ClassName: DicVipgradeService 
 * @Description:  浼氬憳绛夌骇绠＄悊
 * @author 830
 * @date 2016骞�10鏈�26鏃� 
 *  
 */
package com.fzrskj.cloud.free.dic.service;

import com.fzrskj.cloud.free.consts.AppCode;
import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.dic.bean.DicVipgrade;
import com.fzrskj.cloud.free.dic.utils.DicVipgradeUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.XmlCodeParser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DicVipgradeService extends CObject{
	
	/**
	 * to query Vipgrade info
	 * @param req
	 * @param resp
	 * @param p={"levelCode":"levelCode"}
	 * @param sid
	 * @return
	 */
	public String queryVipgrade(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String levelCode = CJsonTool.getStrValue(p, "CJsonTool", null);
		int page = CJsonTool.getIntValue(p, "page", 1);
		int pagesize = CJsonTool.getIntValue(p, "pagesize", CConsts.PAGE_SIZE);
		//定义参数
		Map<String, Object> map = new HashMap<String, Object>();
		if(!CStrTool.isBlankStr(levelCode)){map.put("levelcode", levelCode);}
		DicVipgradeUtils obj = new DicVipgradeUtils();
		int rows = obj.queryVipgrade(map);
		String code = obj.getErrCode();
		if(!code.equals(SysErr.CODE_SUCCESS)){
			obj.dispose();
			return XmlCodeParser.formatResultTips(false,code);
		}
		if (rows<(page-1)*pagesize){
			obj.dispose();
			return XmlCodeParser.formatResultTips(false,SysErr.SYS_DATA_NOT_FOUND);
		}
		int pages = Math.round((rows+pagesize-1)/pagesize);
		//to query rows record
		List<DicVipgrade> vipgrade = obj.queryDicVipgradeList(map, page, pagesize);
		code = obj.getErrCode();
		obj.dispose();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}

		return XmlCodeParser.formatResultPageData(code, page, pages, pagesize, rows, CJsonTool.listBeanToJson(vipgrade));
	}

	/**
	 * 查询会员等级列表
	 * @param req
	 * @param resp
	 * @param p={"levelCode":"levelCode"}
	 * @param sid
	 * @return
	 */
	public String queryVipgradeList(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		DicVipgradeUtils dvUtils=new DicVipgradeUtils();
		List<DicVipgrade> list=dvUtils.querySysVipgrade();
		String code=dvUtils.getErrCode();
		if (list==null) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultData(true, code, CJsonTool.listBeanToJson(list));
	}

	/**
	 * to add OsImage info
	 * @param req
	 * @param resp
	 * @param p={"levelCode":"会员等级代码","levelName":"会员级别名","maxCharge":"最大充值","minCharge":"最小充值","rankIco":"会员级别图标","rebate":"折扣","showIndex":"showIndex"}
	 * @param sid
	 * @return
	 */
	public String addVipgrade(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String levelCode= CJsonTool.getStrValue(p, "levelCode", null);//会员等级代码
		String levelName= CJsonTool.getStrValue(p, "levelName", null);//会员级别名
		Double maxCharge= (double) CJsonTool.getFloatValue(p, "maxCharge",0);//最大充值
		Double minCharge= (double) CJsonTool.getFloatValue(p, "minCharge",0);//最小充值
		String rankIco= CJsonTool.getStrValue(p, "rankIco", null);//会员级别图标
		Double rebate= (double) CJsonTool.getFloatValue(p, "rebate",0);//折扣
		int showIndex= CJsonTool.getIntValue(p, "showIndex", 0);//

		if (CStrTool.isBlankStr(levelCode) || CStrTool.isBlankStr(levelName)){
				return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
			}
		DicVipgradeUtils obj = new DicVipgradeUtils();
		 if(obj.queryDicVipgradeByCode(levelCode) != null){
			 return XmlCodeParser.formatResultTips(false, AppCode.VIPGRADE_EXISTS);
		 }
		Map<String, Object> os = new HashMap<String, Object>();
		os.put("levelCode", levelCode);
		os.put("levelName", levelName);
		os.put("maxCharge", maxCharge);
		os.put("minCharge", minCharge);
		os.put("rankIco", rankIco);
		os.put("rebate", rebate);
		os.put("showIndex", showIndex);

		int ret=obj.addVipgrade(os);
		boolean success = (ret>0);
		String code = obj.getErrCode();
		obj.dispose();

		return XmlCodeParser.formatResultTips(success, code);
	}

	/**
	 * to update OsImage info
	 * @param req
	 * @param resp
	 * @param p={"id":"ID","levelCode":"会员等级代码","levelName":"会员级别名","maxCharge":"最大充值","minCharge":"最小充值","rankIco":"会员级别图标","rebate":"折扣","showIndex":"showIndex"}
	 * @param sid
	 * @return
	 */
	public String  updateVipgrade(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int Id = CJsonTool.getIntValue(p, "id", 0);
		String levelCode= CJsonTool.getStrValue(p, "levelCode", null);//会员等级代码
		String levelName= CJsonTool.getStrValue(p, "levelName", null);//会员级别名
		Double maxCharge= (double) CJsonTool.getFloatValue(p, "maxCharge",0);//最大充值
		Double minCharge= (double) CJsonTool.getFloatValue(p, "minCharge",0);//最小充值
		String rankIco= CJsonTool.getStrValue(p, "rankIco", null);//会员级别图标
		Double rebate= (double) CJsonTool.getFloatValue(p, "rebate",0);//折扣
		int showIndex= CJsonTool.getIntValue(p, "showIndex", 0);//

		if (CStrTool.isBlankStr(levelCode) || CStrTool.isBlankStr(levelName)){
				return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
			}

		Map<String, Object> os = new HashMap<String, Object>();
		os.put("id", Id);
		os.put("levelCode", levelCode);
		os.put("levelName", levelName);
		os.put("maxCharge", maxCharge);
		os.put("minCharge", minCharge);
		os.put("rankIco", rankIco);
		os.put("rebate", rebate);
		os.put("showIndex", showIndex);
		DicVipgradeUtils obj = new DicVipgradeUtils();
		int ret=obj.uptVipgrade(os);
		boolean success = (ret>0);
		String code = obj.getErrCode();
		obj.dispose();

		return XmlCodeParser.formatResultTips(success, code);
	}

	/**
	 * to delete OsImage info
	 * @param req
	 * @param resp
	 * @param p={"id":"ID"}
	 * @param sid
	 * @return
	 */
	public String  delVipgrade(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String Id= CJsonTool.getStrValue(p, "id", "");//主机类型
		if (CStrTool.isBlankStr(Id)){
				return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}
		DicVipgradeUtils obj = new DicVipgradeUtils();
		int ret=obj.delDicVipgrade(CStrTool.strToIntDef(Id, 0));
		boolean success = (ret>0);
		String code = obj.getErrCode();
		obj.dispose();

		return XmlCodeParser.formatResultTips(success, code);
	}

	/**
	 * 根据会员Id查询会员等级
	 * @param req
	 * @param resp
	 * @param p={}
	 * @param sid
	 * @return
	 */
	public String queryDicVipgradeByUid(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int uid = CloudContextUtils.getSession().getUID();

		DicVipgradeUtils dvUtils=new DicVipgradeUtils();
		DicVipgrade view=dvUtils.queryDicVipgradeByUid(uid);
		String code=dvUtils.getErrCode();
		if (view==null) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultData(true, code, CJsonTool.beanToJson(view));
	}
}